package com.resource.project.common;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.resource.project.entity.system.Hr;
import com.resource.project.mapper.HrMapper;
import com.resource.project.os.HrVo;
import com.resource.project.service.system.service.IHrRoleService;
import com.resource.project.service.system.service.os.HrRoleDTO;
import com.resource.util.SpringContextHolder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

import java.util.List;
import java.util.stream.Collectors;


public class HrUtils {
    public static Hr getCurrentHr() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        HrMapper hrMapper = (HrMapper) SpringContextHolder.getBean(HrMapper.class);
        Hr hr = hrMapper.selectOne(Wrappers.<Hr>lambdaQuery().eq(Hr::getUsername, user.getUsername()));
        return  hr;
    }

    public static HrVo getDetailHr() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        HrMapper hrMapper = (HrMapper) SpringContextHolder.getBean(HrMapper.class);
        IHrRoleService hrRoleService = (IHrRoleService) SpringContextHolder.getBean(IHrRoleService.class);
        Hr hr = hrMapper.selectOne(Wrappers.<Hr>lambdaQuery().eq(Hr::getUsername, user.getUsername()));
        List<HrRoleDTO> roles = hrRoleService.getAllRoleByHrId(hr.getId());
        HrVo hrVo = (HrVo) new HrVo().copyAttr(hr);
        hrVo.setRoleSet(roles.stream().map(HrRoleDTO::getRoleName).collect(Collectors.toList()));
        return  hrVo;
    }
}
